/**
 * 
 */
package com.autosalon.ui.banka;

import java.util.ArrayList;
import java.util.List;

import com.autosalon.DAO.DaoFactory;
import com.autosalon.tools.IPaging;
import com.autosalon.tools.PagingComponent;
import com.autosalon.ui.ContentSwitcher;
import com.autosalon.ui.banka.action.BackButtonAction;
import com.autosalon.ui.banka.action.DeleteBankAction;
import com.autosalon.ui.banka.action.EditBankAction;
import com.autosalon.ui.banka.action.NewBankAction;
import com.autosalon.ui.banka.action.SaveBankAction;
import com.autosalon.ui.banka.state.BankaStateHolder;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

/**
 * @author Ragisel
 * 
 */
public class Banka extends HorizontalLayout implements IPaging {

	private final ContentSwitcher switcher;
	private VerticalLayout contentLayout;
	private List<Object> listBanky;
	private PagingComponent pagingCompBanky;
	private com.autosalon.model.Banka banka;
	private Button btnInsert;
	private Button btnBack;
	private Button btnSave;
	private TextField tfNewJmenoBanky;
	private TextField tfEditJmenoBanky;
	private TextField tfMesicniSplatka;
	private Table tblBanky;
	private Form formNewBank;
	private Form formEditBank;
	private Long id_banky;
	private int id;

	public Banka(ContentSwitcher switcher) {
		this.switcher = switcher;
		init();
	}

	private void init() {
		try {

			id = getSwitcher().getMainWindow().getZakaznik().getRole().getId()
					.intValue();
		} catch (NullPointerException e) {
			id = 2;
		}
		initNewBankForm();
		initEditBankForm();

		pagingCompBanky = new PagingComponent(this);
		contentLayout = new VerticalLayout();

		btnInsert = new Button("Nová banka");
		btnInsert.addListener(new NewBankAction(this));

		IndexedContainer ic = new IndexedContainer();
		ic.addContainerProperty("NAZEV", String.class, null);
		if (id == 3) {
			ic.addContainerProperty("EDIT", Button.class, null);
			ic.addContainerProperty("DELETE", Button.class, null);
		}
		tblBanky = new Table("Banky");
		tblBanky.setContainerDataSource(ic);
		tblBanky.setColumnWidth("NAZEV", 150);
		naplnTabulkuObjednavky();

		contentLayout.setSpacing(true);
		contentLayout.addComponent(pagingCompBanky);
		contentLayout.addComponent(tblBanky);
		contentLayout.addComponent(btnInsert);

		addComponent(contentLayout);

		BankaStateHolder.setState(BankaStateHolder.VIEW_BANKA_STATE,id, this);

	}

	@Override
	public void naplnTabulkuObjednavky() {
		listBanky = new ArrayList<Object>();
		listBanky = DaoFactory.getinstance().getBanka()
				.getAll(pagingCompBanky.getFrom(), pagingCompBanky.getTo());
		tblBanky.removeAllItems();

		for (Object ob : listBanky) {
			banka = (com.autosalon.model.Banka) ob;

			if (id == 3) {
				Button btnEditBank;
				btnEditBank = new Button("Editovat");
				btnEditBank.setStyleName(Button.STYLE_LINK);
				btnEditBank.addListener(new EditBankAction(this));
				btnEditBank.setData(banka.getId());

				Button btnDeleteBank;
				btnDeleteBank = new Button("Smazat");
				btnDeleteBank.setStyleName(Button.STYLE_LINK);
				btnDeleteBank.addListener(new DeleteBankAction(this));
				btnDeleteBank.setData(banka.getId());
				tblBanky.addItem(new Object[] { banka.getNazevBanky(),
						btnEditBank, btnDeleteBank }, null);
			} else
				tblBanky.addItem(new Object[] { banka.getNazevBanky() }, null);
		}
	}

	//

	public void initEditBankForm() {
		btnBack = new Button("Zpet");
		btnBack.addListener(new BackButtonAction(this));
		btnSave = new Button("Uložit");
		btnSave.addListener(new SaveBankAction(this));
		tfEditJmenoBanky = new TextField("Jméno banky: ");

		formEditBank = new Form();
		formEditBank.setCaption("Editace banky");
		formEditBank.setDescription("\n" + "Formulář pro editaci banky");
		formEditBank.getLayout().addComponent(tfEditJmenoBanky);
		formEditBank.getLayout().addComponent(btnBack);
		formEditBank.getLayout().addComponent(btnSave);

		addComponent(formEditBank);

	}

	public void initNewBankForm() {

		btnBack = new Button("Zpet");
		btnBack.addListener(new BackButtonAction(this));

		btnSave = new Button("Uložit");
		btnSave.addListener(new SaveBankAction(this));

		tfNewJmenoBanky = new TextField();

		formNewBank = new Form();
		formNewBank.setSizeFull();
		formNewBank.setCaption("Nová banka");
		formNewBank.setDescription("\n" + "Formulář pro přidání nové banky");

		formNewBank.getLayout().addComponent(tfNewJmenoBanky);
		formNewBank.getLayout().addComponent(btnBack);
		formNewBank.getLayout().addComponent(btnSave);

		addComponent(formNewBank);

	}

	public VerticalLayout getContentLayout() {
		return contentLayout;
	}

	public void setContentLayout(VerticalLayout contentLayout) {
		this.contentLayout = contentLayout;
	}

	public Table getTblBanky() {
		return tblBanky;
	}

	public void setTblBanky(Table tblBanky) {
		this.tblBanky = tblBanky;
	}

	public Button getBtnInsert() {
		return btnInsert;
	}

	public void setBtnInsert(Button btnInsert) {
		this.btnInsert = btnInsert;
	}

	public List<Object> getListBanky() {
		return listBanky;
	}

	public void setListBanky(List<Object> listBanky) {
		this.listBanky = listBanky;
	}

	public PagingComponent getPagingCompBanky() {
		return pagingCompBanky;
	}

	public void setPagingCompBanky(PagingComponent pagingCompBanky) {
		this.pagingCompBanky = pagingCompBanky;
	}

	public com.autosalon.model.Banka getBanka() {
		return banka;
	}

	public void setBanka(com.autosalon.model.Banka banka) {
		this.banka = banka;
	}

	public Button getBtnBack() {
		return btnBack;
	}

	public void setBtnBack(Button btnBack) {
		this.btnBack = btnBack;
	}

	public Button getBtnSave() {
		return btnSave;
	}

	public void setBtnSave(Button btnSave) {
		this.btnSave = btnSave;
	}

	public TextField getTfJmenoBanky() {
		return tfNewJmenoBanky;
	}

	public void setTfJmenoBanky(TextField tfJmenoBanky) {
		this.tfNewJmenoBanky = tfJmenoBanky;
	}

	public TextField getTfMesicniSplatka() {
		return tfMesicniSplatka;
	}

	public void setTfMesicniSplatka(TextField tfMesicniSplatka) {
		this.tfMesicniSplatka = tfMesicniSplatka;
	}

	public Form getFormNewBank() {
		return formNewBank;
	}

	public void setFormNewBank(Form formNewBank) {
		this.formNewBank = formNewBank;
	}

	public Form getFormEditBank() {
		return formEditBank;
	}

	public void setFormEditBank(Form formEditBank) {
		this.formEditBank = formEditBank;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public ContentSwitcher getSwitcher() {
		return switcher;
	}

	public Long getId_banky() {
		return id_banky;
	}

	public void setId_banky(Long id_banky) {
		this.id_banky = id_banky;
	}

	public TextField getTfEditJmenoBanky() {
		return tfEditJmenoBanky;
	}

	public void setTfEditJmenoBanky(TextField tfEditJmenoBanky) {
		this.tfEditJmenoBanky = tfEditJmenoBanky;
	}

}
